﻿using System;
using System.Web.Mvc;
using UI.Nafania.Controllers.Base;
using WebService.Nafania.BussinesLayer.Model.Generator;
using WebService.Nafania.ViewLayer.Model;

namespace UI.Nafania.Controllers
{
	public class HomeController : BaseController
	{
		#region Index

		public ActionResult Index()
		{
			return View();
		}

		#endregion

		#region Object

		public void ObjectAsync(string alias, string name, long? version)
		{
			AsyncManager.OutstandingOperations.Increment();

			Proxy.GetVersionObjectCompleted += (sender, e) =>
			{
				AsyncManager.Parameters["line"] = e.Result;
				AsyncManager.OutstandingOperations.Decrement();
			};
			Proxy.GetVersionObjectAsync(alias, name, version);
		}

		public ActionResult ObjectCompleted(LineResult line)
		{
			ViewData.Model = line;
			return View();
		}

		#endregion

		#region BlockData

		public void BlockDataAsync(string alias, string name, long? version, Guid blockId)
		{
			AsyncManager.OutstandingOperations.Increment();

			Proxy.GetBlockDataCompleted += (sender, e) =>
			{
				AsyncManager.Parameters["block"] = e.Result;
				AsyncManager.OutstandingOperations.Decrement();
			};
			Proxy.GetBlockDataAsync(alias, name, version, blockId);
		}

		public ActionResult BlockDataCompleted(BlockInfo block)
		{
			return PartialView(block);
		}

		[HttpPost, ActionName("BlockData")]
		public void BlockDataPostAsync(BlockInfo blockInfo)
		{
			AsyncManager.OutstandingOperations.Increment();

			Proxy.SaveBlockDataCompleted += (sender, e) =>
			{
				AsyncManager.Parameters["block"] = e.Result;
				AsyncManager.OutstandingOperations.Decrement();
			};

			Proxy.SaveBlockDataAsync(blockInfo.Alias, blockInfo.Name, blockInfo.Version, RequestParams);
		}


		public ActionResult BlockDataPostCompleted(BlockInfo block)
		{
			return RedirectToAction("Object", new {alias = block.Alias, name = block.Name, version = block.Version });
		}

		#endregion
	}
}
